package com.tbit.main.pojo;

import com.tbit.main.exception.BaseException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 云bms配置实体类
 *
 * @Author:BUGTian
 * @DATE: 2022/7/15
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BmsConfig {

    private static final String COUNT_KEY = "count";
    private static final String LIFE_POOR_HEALTH_KEY = "lifePoorHealth";
    private static final String ADVICE_RECYCLE_HEALTH_KEY = "lifePoorHealth";
    private static final String SCRAP_HEALTH_KEY = "scrapHealth";
    /**
     * 授权数量
     */
    private Integer count;
    /**
     * 续航差健康度
     */
    private Integer lifePoorHealth;
    /**
     * 建议回收健康度
     */
    private Integer adviceRecycleHealth;
    /**
     * 报废健康度
     */
    private Integer scrapHealth;

    public static void main(String[] args) {
       /* BmsConfig bmsConfig = new BmsConfig(100, 30, 20, 5);
        //转换为key=value形式
        String bmsConfigStr = bmsConfig.toBmsConfigStr();
        System.out.println(bmsConfigStr);
        //转换为对象
        BmsConfig bmsConfigByStr = new BmsConfig().getBmsConfigByStr(bmsConfigStr);
        System.out.println(bmsConfigByStr);*/

    }

    /**
     * 转成key=value形式保存到配置表
     *
     * @return
     */
    public String toBmsConfigStr() {
        return String.format("%s=%s&%s=%s&%s=%s&%s=%s",
                COUNT_KEY, count == null ? 0 : count,
                LIFE_POOR_HEALTH_KEY, lifePoorHealth == null ? 0 : lifePoorHealth,
                ADVICE_RECYCLE_HEALTH_KEY, adviceRecycleHealth == null ? 0 : adviceRecycleHealth,
                SCRAP_HEALTH_KEY, scrapHealth == null ? 0 : scrapHealth
        );
    }

    public void param() {
        if (lifePoorHealth >= 100 || scrapHealth <= 0 || lifePoorHealth <= adviceRecycleHealth || adviceRecycleHealth <= scrapHealth) {
            throw new BaseException("bms.param.error");
        }
    }

    /**
     * 根据key=value形式的字符串，转换为实体类
     *
     * @param bmsConfigStr
     * @return
     */
    public BmsConfig getBmsConfigByStr(String bmsConfigStr) {
        String[] items = bmsConfigStr.split("&");
        setCount(Integer.parseInt(items[0].split("=")[1]));
        setLifePoorHealth(Integer.parseInt(items[1].split("=")[1]));
        setAdviceRecycleHealth(Integer.parseInt(items[2].split("=")[1]));
        setScrapHealth(Integer.parseInt(items[3].split("=")[1]));
        return this;
    }

    /**
     * 根据key=value形式的字符串，获取参数
     *
     * @param bmsConfigStr
     * @return
     */
    public Integer getBmsConfig(String bmsConfigStr) {
        return Integer.parseInt(bmsConfigStr.split("=")[1]);
    }

}
